//
// Created by WenYF on 2016/11/4.
//

#ifndef FZJH_SESSION_H
#define FZJH_SESSION_H

#include "common.h"
#include "packet.h"
#include "parser.h"
#include "util.h"
#include "io-posix.h"
#ifdef __cplusplus  
extern "C" {  
#endif  

#define NET_IO_BUF_SIZE 4096

// connect timeout second
#define CONNECT_TIMEOUT 30

// below is the connect state
#define CONNECT_STATE_OPEN    2001
#define CONNECT_STATE_SUSPEND 2002
#define CONNECT_STATE_CLOSE   2003


// below type is for j_connectErrorCallback
#define TYPE_HOST_ERROR     1001
#define TYPE_CONNECT_ERROR  1002
#define TYPE_IO_ERROR       1003
#define TYPE_AUTH_ERROR     1004
#define TYPE_CONNECT_TIOMEOUT   1005
#define TYPE_CONNECT_UNKNOW_ERROR   1006

// recv message callback
typedef void (if_messageRecvCallback)(void* userdata, ifmessage *message);
// recv event callback
typedef void (if_eventRecvCallback)(void* userdata, ifevent *event);
// connect successed callback
typedef void (if_connectSuccessedCallback)(void* userdata, char* extra);
// connect faild callback
typedef void (if_connectErrorCallback)(void* userdata, int type, char *reason);
// when recv a disconnect cmd from server, disconnect will callback
// 断开连接，重新连接可以直接连，但是系统不会释放sessin
typedef void (if_disconnectCallback)(void* userdata, char *reason);

void* if_session_new(char *serverHost, unsigned short port, char *username, char *token, byte key, int pingInteval);
void if_session_setCallback(void *session, void *userdata, if_messageRecvCallback *mrcb, if_eventRecvCallback *ercb
        , if_connectSuccessedCallback *cscb, if_connectErrorCallback *cecb
        , if_disconnectCallback *dcb);
void if_session_connect(void *session, int timeout);
void if_session_sendMessage(void *session, ifmessage *message);
void if_session_sendEvent(void *session, ifevent *event);
// immediately为false:可能会出现，还在读取数据，结果断开了连接
// 断开连接，先发送关闭会话消息，收到后在会调，如果网络不好，那么就可能收不到，这样你要超市处理
// immediately为true:立即断开连接
void if_session_disconnect(void *session, BOOL immediately);
void if_session_delete(void *session);

int if_session_getState(void *session);
int if_session_getMsDelay(void *session);
int if_session_getDownloadCount(void *session);
int if_session_getUploadCount(void *session);
unsigned short if_session_getPort(void *session);
char* if_session_getServerHost(void *session);
char* if_session_getServerName(void *session);
char* if_session_getUsername(void *session);
ifulaid* if_session_getServerUid(void *session);
ifulaid* if_session_getLocalUid(void *session);
#ifdef __cplusplus  
}  
#endif 
#endif //FZJH_SESSION_H
